/*
 * Created on 2004-06-03
 */
package org.opencube.oms.mapping.generator;

import info.fingo.util.Text;

import java.util.ArrayList;

import org.opencube.oms.mapping.Entity;
import org.opencube.oms.mapping.Finder;
import org.opencube.oms.meta.Scheme;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO: comment
 */
public class TemplateUtil
{
	public String upCase( String s )
	{
		String result = s;
		if( s != null && s.trim().length() > 0 )
		{
			s = s.trim();
			result = s.substring( 0, 1 ).toUpperCase();
			if( s.length() > 0 )
			{
				result += s.substring( 1 );
			}
		}
		return result;
	}
	
	public String lowCase( String s )
	{
		String result = s;
		if( s != null && s.trim().length() > 0 )
		{
			s = s.trim();
			result = s.substring( 0, 1 ).toLowerCase();
			if( s.length() > 0 )
			{
				result += s.substring( 1 );
			}
		}
		return result;
	}
	
	public String getPackage( String basePackage, Scheme s )
	{
		String result = ( basePackage == null ? "" : basePackage ) + Text.substitute( s.getNamespace().getPath(), ".", "_" ); 
		result = Text.substitute( result, "/", "." );
		result = Text.substitute( result, "-", "_" );
		if( result.startsWith( "." ) )
		{
			result = result.substring( 1 );
		}
		
		return result;
	}
	
	public String getEntityExtends( String basePackage, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = Entity.class.getName();
		}
		else
		{
			result = getEntityTypeName( basePackage, s.getParentScheme() );
		}
		
		return result;
	}
	
	public String getFinderExtends( String basePackage, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = Finder.class.getName();
		}
		else
		{
			result = getFinderTypeName( basePackage, s.getParentScheme() );
		}
		
		return result;
	}

	public String getBaseEntityExtends( String basePackage, Scheme s )
	{
		return getPackage( basePackage, s ) + ".Base" + upCase( s.getName() );
	}

	public String getBaseFinderExtends( String basePackage, Scheme s )
	{
		return getPackage( basePackage, s ) + ".Base" + upCase( s.getName() + "Finder");
	}

	public String getEntityTypeName( String basePackage, Scheme s )
	{
		
		return getPackage( basePackage, s ) + "." + upCase( s.getName() );
	}
	
	public String getFinderTypeName( String basePackage, Scheme s )
	{
		
		return getPackage( basePackage, s ) + "." + upCase( s.getName() ) + "Finder";
	}

	public String getConstant( String s )
	{
		String result = "";
		boolean lastLower = false;
		for (int i = 0; s != null && i < s.length(); i++)
		{
			char c = s.charAt( i );
			
			if( c >= 'A' && c <= 'Z')
			{
				if( lastLower )
				{
					result += "_";
				}
			}
			else
			{
				lastLower = true;
			}
			result += Character.toUpperCase( c );
		}
		return result;
	}
	
	private ArrayList getAllSchemes( Scheme[] s )
	{
		ArrayList l = new ArrayList();
		for (int i = 0; s != null && i < s.length; i++)
		{
			l.add( s[ i ] );
			l.addAll( getAllSchemes( s[ i ].getChildSchemes() ) );
		}
		return l;
	}
	
	public Scheme[] getSchemeTree( Scheme s )
	{
		ArrayList schemes = getAllSchemes( new Scheme[]{s} );
System.out.println( s.getNamespace().getPath() + "/" + s.getName() + ": " + schemes.size() );
		return (Scheme[]) schemes.toArray(new Scheme[schemes.size()]);
	}
}
